package com.epfresh.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.epfresh.R;
import com.epfresh.api.universalimageloader.core.ImageLoader;
import com.epfresh.bean.HomeColumnBean;

import multitype.ItemViewBinder;

/**
 * Author: Chen
 * Date: 2020/5/26 18:03
 * Des:
 */
public class ColumnDetailProductViewBinder extends ItemViewBinder<HomeColumnBean.ProductsBean,ColumnDetailProductViewBinder.ViewHolder> {
    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View itemView = inflater.inflate(R.layout.item_column_product,parent,false);
        return new ViewHolder(itemView);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull final HomeColumnBean.ProductsBean item) {
        if (!TextUtils.isEmpty(item.getThumbnail())) {
            ImageLoader.getInstance().displayImage(item.getThumbnail(),holder.img_product);
        }
        holder.txt_product_name.setText(item.getProductParseDto().getTitle());
        if (item.getName() != null && !item.getName().equals("默认")) {
            holder.txt_pack.setText(item.getName() + "(" + item.getPack() + ")");
        } else {
            holder.txt_pack.setText(item.getPack());
        }
        holder.txt_store_name.setText(item.getStoreName());
        holder.txt_charge_unit.setText("¥" + item.getProductSpecPrice().getPrice() + "/" + item.getChargeUnit());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(item);
                }
            }
        });
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private TextView txt_product_name,txt_pack,txt_store_name,txt_charge_unit;
        private ImageView img_product;

        ViewHolder(View itemView) {
            super(itemView);

            txt_product_name = itemView.findViewById(R.id.txt_product_name);
            txt_pack = itemView.findViewById(R.id.txt_pack);
            txt_store_name = itemView.findViewById(R.id.txt_store_name);
            txt_charge_unit = itemView.findViewById(R.id.txt_charge_unit);
            img_product = itemView.findViewById(R.id.img_product);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(HomeColumnBean.ProductsBean item);
    }

}
